<?php
namespace Foxframe;
class Uri
{
	private static $instance;
	protected $application;
	protected $controller;
	protected $method;
	protected $params = array();

	private function __construct()
	{
		return $this;
	}

	public static function getInstance()
	{
		if (!isset(self::$instance))
			self::$instance	= new self();

		return self::$instance;
	}

	public function set($param, $value)
	{
		$this->$param	= $value;
	}

	public function getUri()
	{
		$requestURI	= explode('/', str_replace('?','',$_SERVER['REQUEST_URI']));
		$scriptName	= explode('/', str_replace('?','',$_SERVER['SCRIPT_NAME']));

		for ($i= 0;$i < count($scriptName);$i++)
		{
			if ($requestURI[$i] == $scriptName[$i])
				unset($requestURI[$i]);
		}

		$request	= array_values($requestURI);

		//Fazendo leitura da url não amigável
		if (!count($request) || (count($request) == 1) && empty($request[0]))
		{
			foreach ($_REQUEST as $key => $value)
			{
				if ($key == 'application')
					$this->application		= ucfirst($value);
				else if ($key == 'controller')
					$this->controller		= $value;
				else if ($key == 'method')
					$this->method			= $value;
				else
					$this->params[$key]		= $value;
			}
		}
		else
		{
			if (isset($request[0]) && !empty($request[0]))
				$this->application	= ucfirst($request[0]);
			if (isset($request[1]) && !empty($request[1]))
				$this->controller	= $request[1];
			if (isset($request[2]) && !empty($request[2]))
				$this->method		= $request[2];

			for ($i=0; $i<(count($request)); $i++)
			{
				if ($i == 0)
					$this->params['application']	= $request[$i];
				else if ($i == 1)
					$this->params['controller']		= $request[$i];
				else if ($i == 2)
					$this->params['method']			= $request[$i];
				else if ($i % 2 != 0)
					$this->params[$request[$i]] 	= isset($request[$i+1]) ? $request[++$i] : null;
			}
		}

		if (!isset($this->application))
			$this->application	= 'Login';
		if (!isset($this->controller))
			$this->controller	= 'index';
		if (!isset($this->method))
			$this->method		= 'index';

		return $this;
	}

	public function get($param)
	{
		return $this->$param;
	}

	public function getUrl($full = false)
	{
		$retorno = BASE_PATH;
		if ($this->get('application'))
			$retorno	.= $this->get('application');
		if ($this->get('controller'))
			$retorno	.= '/'.$this->get('controller');
		if ($this->get('method'))
			$retorno	.= '/'.$this->get('method');
		return ($retorno);
	}

	public function generateUrl($application = null, $controller = null, $method = null, $params = null)
	{

		$retorno = BASE_PATH;

		if (!is_null($application))
			$retorno.= $application.'/';
		else
			$retorno.= $this->get('application').'/';

		if (!is_null($controller))
			$retorno.= $controller.'/';
		else
			$retorno.= $this->get('controller').'/';

		if (!is_null($method))
			$retorno.= $method;
		else
			$retorno.= $this->get('method');


		$request	= array();
		$tmp	= explode('?',$params);
		$params	= explode('&', $tmp[count($tmp) -1]);
		foreach ($params as $param)
		{
			$new	= explode('=', $param);
			if (!empty($new[0]))
				$retorno.= '/'.$new[0].'/'.$new[1];
		}

		return $retorno;
	}

	public function getOrigem()
	{
		if (isset($_SERVER['HTTP_REFERER']))
			return $_SERVER['HTTP_REFERER'];
	}

	public function hasAccess($application, $controller, $method)
	{
		$acessoLiberado = true;
		if (isset($_SESSION[SESSION_LOGIN]['aplicacoes']))
		{
			foreach ($_SESSION[SESSION_LOGIN]['aplicacoes'] as $kApp => $vApp)
			{
				if (strtolower($vApp['ds_path']) == strtolower($application))
				{
					foreach ($_SESSION[SESSION_LOGIN]['aplicacoes'][$kApp]['perfilacoes'] as $kAcao => $vAcao)
					{
						if (strtolower($vAcao['ds_controller']) == strtolower($controller)
								&& strtolower($vAcao['ds_method']) == strtolower($method))
						{
							$acessoLiberado = false;

							if (isset($_SESSION[SESSION_LOGIN]['perfis']))
							{
								foreach ($_SESSION[SESSION_LOGIN]['perfis'] as $perfil)
								{
									if ($vAcao['perfil']['id_perfil'] == $perfil['id_perfil'])
										return true;
								}
							}
						}
					}
				}
			}
		}
		return $acessoLiberado;
	}
}